<template>
  <div>
    <el-button
      type="primary"
      icon="el-icon-plus"
      style="margin: 10px 0px"
      @click="showDialog"
      >添加</el-button
    >
    <!-- 
        width:对应列的宽度
        align:标题的对其方式
     -->
    <el-table style="width: 100%" border :data="list">
      <el-table-column type="index" label="序号" width="80px" align="center">
      </el-table-column>
      <el-table-column
        prop="tmName"
        label="品牌名称"
        width="width"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="data"
        label="品牌Logo"
        width="width"
        align="center"
      >
        <!-- 插槽 -->
        <template slot-scope="{ row, $index }">
          <img :src="row.logoUrl" style="width: 100px; heigth: 100px" />
        </template>
      </el-table-column>
      <el-table-column prop="data" label="操作" width="width" align="center">
        <template slot-scope="{ row, $index }">
          <el-button
            type="warning"
            icon="el-icon-edit"
            @click="updateTradeMark(row)"
            >修改</el-button
          >
          <el-button type="danger" icon="el-icon-delete" @click="tradeMakeDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 
        分页器
        1.当前页数 2.多少条数据 3.每一页展示多少个 4.连续的页码数
        current-page 当前的页数
        :page-size  每页显示多少条数据
        :total 总条数
         :page-sizes="[3, 5, 10]" 
     -->
    <el-pagination
      style="margin-top: 20px; textalign: center"
      @size-change="changelimit"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      layout=" prev, pager, next, jumper,->,total, sizes"
      :total="total"
    >
    </el-pagination>
    <!-- 弹框 -->
    <el-dialog
      :title="tmForm.id ? '修改品牌' : '添加品牌'"
      :visible.sync="dialogFormVisible"
    >
      <!-- 将表单的数据收集到tmForm对象中 -->
      <!-- Form 组件提供了表单验证的功能，
      只需要通过 rules 属性传入约定的验证规则，并将 Form-Item 的 prop 属性设置为需校验的字段名即可。 -->
      <el-form style="width: 80%" :model="tmForm" :rules="rules" ref="ruleForm">
        <el-form-item label="添加品牌" label-width="100px" prop="tmName">
          <el-input autocomplete="off" v-model="tmForm.tmName"></el-input>
        </el-form-item>
        <el-form-item label="品牌LOGO" label-width="100px" prop="logoUrl">
          <el-upload
            class="avatar-uploader"
            action="/dev-api/admin/product/fileUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="tmForm.logoUrl" :src="tmForm.logoUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div slot="tip" class="el-upload__tip">
              只能上传jpg/png文件，且不超过500kb
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="AddOrUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "tradeMake",
  data() {
    return {
      //当前是第几页
      page: 1,
      //一页展示多少条数据
      limit: 3,
      //数据的总条数
      total: 0,
      //数据列表
      list: [],
      //对话框显示与隐藏的控制
      dialogFormVisible: false,
      //关于图片
      imageUrl: "",
      //收集表单数据
      tmForm: {
        tmName: "",
        logoUrl: "",
      },
      rules: {
        tmName: [
          //required 必须要验证的字段，message:错误的提示信息，trigger用户的行为事件
          { required: true, message: "请输入产品名称", trigger: "blur" },
          //长度2-10
          {
            min: 2,
            max: 10,
            message: "长度在 2 到 10 个字符",
            trigger: "change",
          },
        ],
        logoUrl: [{ required: true, message: "请选择品牌logo" }],
      },
    };
  },
  mounted() {
    this.getPageList();
    //  console.log(this.$API);
  },
  //获取商品列表
  methods: {
    async getPageList() {
      //解构出page,limit
      const { page, limit } = this;
      let result = await this.$API.tradeMark.reqTradeMarkList(page, limit);

      if (result.code == 200) {
        this.total = result.data.total;
        this.list = result.data.records;
      }
    },
    handleCurrentChange(page) {
      this.page = page;
      this.getPageList();
    },
    changelimit(limit) {
      this.limit = limit;
      this.getPageList();
    },
    showDialog() {
      //显示对话框
      this.dialogFormVisible = true;
      //清除数据
      this.tmForm.tmName = "";
      this.tmForm.logoUrl = "";
    },
    //添加数据按钮
    AddOrUpdate() {
      this.$refs.ruleForm.validate(async (success) => {
        if (success) {
          this.dialogFormVisible = false;
          let result = "";
          if (!this.tmForm.id) {
            result = await this.$API.tradeMark.reqAdd(this.tmForm);
          } else {
            result = await this.$API.tradeMark.reqUpdate(this.tmForm);
          }

          if (result.code == 200) {
            //添加品牌成功之后，重新获取数据列表
            this.getPageList();
            //弹出添加成功或者修改成功
            this.$message({
              message: this.tmForm.id ? "修改成功" : "添加成功",
              type: "success",
            });
          }
        }
      });
    },
    //修改对话框
    updateTradeMark(row) {
      this.dialogFormVisible = true;
      this.tmForm = { ...row };
      console.log(this.tmForm);
    },
    //上传图片之后的回调
    handleAvatarSuccess(res, file) {
      this.tmForm.logoUrl = res.data;
    },
    //上传图片之前的回调
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
     tradeMakeDelete(row) {
       this.$confirm(`此操作将删除${row.tmName}, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          let result =await this.$API.tradeMark.reqDelete(row.id)
          if(result.code == 200) {
             this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getPageList()
          }
         
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    }
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
